<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-button type="primary" @click="handleForm(false, {})" :disabled="isDisabledAuth(Auth.saveAgreement)">
          <Icon icon="i-ant-design:plus-outlined" /> {{ t("新增") }}
        </a-button>
      </template>
    </BasicTable>
    <InputForm @register="registerInputForm" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" name="Agreement" setup>
  import { ref } from "vue"
  import { Auth } from "/@/enums/authEnum"
  import { BasicTable, BasicColumn, useTable } from "/@/components/Table"
  import { useModal } from "/@/components/Modal"
  import { useI18n } from "/@/hooks/web/useI18n"
  import { useGlobSetting } from "/@/hooks/setting"
  import { columns, searchFormSchema } from "./data"
  import { pageAgreement, deleteAgreement } from "./api"
  import { useMessage } from "/@/hooks/web/useMessage"
  import InputForm from "./Form.vue"
  import { usePermission } from "/@/hooks/web/usePermission"
  import { Icon } from "/@/components/Icon"
  import { useWangEditor } from "/@/components/My/WangEditor"
  useWangEditor()

  const { t } = useI18n("")
  const { createMessage } = useMessage()
  const { isDisabledAuth } = usePermission()
  const glob = useGlobSetting()
  const [registerInputForm, { openModal: openInputForm }] = useModal()

  const actionColumn: BasicColumn = {
    width: 130,
    title: "操作",
    dataIndex: "action",
    fixed: "right",
    slot: "tableActions",
    actions: (record: Recordable) => [
      {
        label: "删除",
        color: "error",
        ifShow: record.sendStatus != 1,
        auth: Auth.deleteAgreement,
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
      {
        label: "编辑",
        auth: Auth.updateAgreement,
        onClick: handleForm.bind(null, true, record),
      },
    ],
    dropDownActions: (record: Recordable) => [],
  }

  const [registerTable, { reload, expandAll, collapseAll, expandCollapse }] = useTable({
    title: "协议管理",
    api: pageAgreement,
    columns: columns,
    bordered: false,
    beforeFetch: (params) => {
      return params
    },
    formConfig: {
      baseColProps: { lg: 6, md: 12 },
      labelWidth: 90,
      schemas: searchFormSchema,
      showResetButton: false,
      showSubmitButton: false,
    },
    showTableSetting: false,
    showIndexColumn: false,
    useSearchForm: true,
    pagination: true,
    canResize: true,
    rowKey: "id",
    actionColumn,
  })

  const handleForm = (isUpdate, record) => openInputForm(true, { record, isUpdate })
  const handleDelete = async (record) => await deleteAgreement({ id: record.id }, reload)
  const handleDetail = (record) => openFormDetail(true, { record })
  const handleSuccess = () => reload()
</script>
